{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0708a6fc",
   "metadata": {},
   "source": [
    "# Nonlinear Systems of Equations"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "801e120c",
   "metadata": {},
   "source": [
    "## Step #1 问题定义"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a238945f",
   "metadata": {},
   "source": [
    "### 求解的方程组\n",
    "$$\n",
    "\\begin{cases}\n",
    "(x_1)^2+(x_1)(x_2)-6\\\\\n",
    "(x_1)+(x_2)^2-5\n",
    "\\end{cases}\n",
    "$$\n",
    "\n",
    "### 写出对应的函数\n",
    "$$\n",
    "f_1(x_1,x_2)=(x_1)^2+(x_1)(x_2)-6\\\\\n",
    "f_2(x_1,x_2)=(x_1)+(x_2)^2-5\n",
    "$$\n",
    "\n",
    "### 写出F(x)\n",
    "$$\n",
    "F(x_1,x_2)=\n",
    "\\left[\n",
    "\\begin{matrix}\n",
    "(x_1)^2+(x_1)(x_2)-6\\\\\n",
    "(x_1)+(x_2)^2-5\n",
    "\\end{matrix}\n",
    "\\right]\n",
    "$$\n",
    "\n",
    "### 写出DF(x)\n",
    "$$\n",
    "DF(x_1,x_2)=\n",
    "\\left[\n",
    "\\begin{matrix}\n",
    "2(x_1)+(x_2) & (x_1)\\\\\n",
    "1 & 2(x_2)\n",
    "\\end{matrix}\n",
    "\\right]\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f672a31",
   "metadata": {},
   "source": [
    "## Step #2 写出F(X)的Python代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "52826076",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "def F(x):\n",
    "    f1 = x[0] ** 2 + x[0] * x[1] - 6\n",
    "    f2 = x[0] + x[1] ** 2 - 5\n",
    "    return np.array([f1,f2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fd99c20b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-6, -5])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([0,0])\n",
    "F(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b461f04",
   "metadata": {},
   "source": [
    "## Step #3 写出DF(X)的Python代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "dc85d33f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def DF(X):\n",
    "    df11 = 2 * X[0] + X[1]\n",
    "    df12 = X[0]\n",
    "    df21 = 1\n",
    "    df22 = 2 * X[1]\n",
    "    return np.array([\n",
    "        [df11,df12],\n",
    "        [df21,df22]\n",
    "    ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "4f5b4de8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 1],\n",
       "       [1, 2]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([1,1])\n",
    "DF(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67edb86e",
   "metadata": {},
   "source": [
    "## Step #4 写出迭代函数的Python代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "de13bb03",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calXnew(xold):\n",
    "    s = np.linalg.inv(DF(xold)) @ F(xold)\n",
    "    xnew = xold - s\n",
    "    return xnew"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "798788fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2., 2.])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([1,1])\n",
    "calXnew(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39add379",
   "metadata": {},
   "source": [
    "## Step #5 进行迭代计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f9945f13",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Iter num: 0 \tXnew: [6. 0.] \terror 18.5\n",
      "Iter num: 1 \tXnew: [ 5. -3.] \terror 5.0\n",
      "Iter num: 2 \tXnew: [ 3.53191489 -1.74468085] \terror 1.8655500226346764\n",
      "Iter num: 3 \tXnew: [ 3.23065152 -1.37940981] \terror 0.11209127860306664\n",
      "Iter num: 4 \tXnew: [ 3.20874836 -1.3389868 ] \terror 0.0010568837580698233\n"
     ]
    }
   ],
   "source": [
    "Xold = np.array([0,1])\n",
    "for i in range(10):\n",
    "    Xnew = calXnew(Xold)\n",
    "    stdError = np.square(Xnew - Xold).mean()\n",
    "#    print(stdError)\n",
    "    if stdError < 1e-5 :\n",
    "        break\n",
    "    Xold = Xnew\n",
    "    print(\"Iter num:\",i,\"\\tXnew:\",Xnew,\"\\terror\",stdError)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9868ff1d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
